package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import com.tarena.lbs.coupon.service.StockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理和优惠券数据业务有关的接口
 */
@RestController
@Slf4j
@Tag(name="优惠券模块")
public class CouponController {
    @Autowired
    private CouponService couponService;
    @Autowired
    private StockService stockService;


    @Operation(summary = "查询当前商家优惠券分页列表")
    @PostMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> pageList() throws BusinessException{
        PageResult<CouponVO> page = couponService.pageList();
        PageResult<CouponVO> query = new PageResult<>();
        page.setPageNo(query.getPageNo());
        page.setPageSize(query.getPageSize());
        page.setTotal(couponService.pageList().getTotal());
        page.setObjects(page.getObjects());
        log.debug("查询当前商家优惠券分页列表成功: {}", query);
        return new Result<>(page);
    }


    @Operation(summary = "新增优惠券")
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> addCoupon(@RequestBody CouponParam couponParam)throws BusinessException{
        log.debug("新增优惠券: {}", couponParam);
        CouponVO couponVO = new CouponVO();
        BeanUtils.copyProperties(couponParam, couponVO);
        couponVO.setBusinessId(1);
        couponVO.setStatus(1);
        couponVO.setCreateAt(new java.util.Date(System.currentTimeMillis()));
        couponVO.setEnableStatus(1);
        couponVO.setUsageNum(0);
        couponService.addCoupon(couponParam);
        log.debug("新增优惠券成功: {}", couponParam);
        stockService.addStock(couponParam.getId(), couponParam.getMaxUsageLimit());
        log.debug("新增库存成功: {}", couponParam);
        return Result.success();
    }

    @Operation(summary = "查询优惠券详情")
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> detail(@PathVariable("id") Integer id)
        throws BusinessException{
        log.debug("查询优惠券详情: {}", id);
        CouponVO detail = couponService.detail(id);
        log.debug("查询优惠券详情成功: {}", id);
        return new Result<>(detail);
    }



    @Operation(summary = "领取优惠券")
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)
        throws BusinessException{
        log.debug("领取优惠券: {}", param);
        couponService.receiveCoupon(param);
        couponService.addStock(param.getCouponId(), param.getMaxUsageLimit());
        stockService.updateStock(param.getCouponId(), param.getMaxUsageLimit());
        log.debug("领取优惠券成功: {}", param);
        return Result.success();
    }


    @Operation(summary = "用户领取优惠券列表")
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> receiveList(@RequestBody UserCouponQuery couponQuery)throws BusinessException{
        log.debug("查询用户优惠券列表: {}", couponQuery);
        PageResult<UserCouponsVO> page = couponService.receiveList(couponQuery);
        page.setPageNo(couponQuery.getPageNo());
        page.setPageSize(couponQuery.getPageSize());
        page.setTotal(couponService.pageList().getTotal());
        page.setObjects(page.getObjects());
        log.debug("查询用户优惠券列表成功: {}", couponQuery);
        return new Result<>(page);
    }


    @Operation(summary = "查看优惠券码的二维码图片")
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> receiveDetail(@RequestBody UserCouponCodeQuery query)
        throws BusinessException{
        log.debug("查看优惠券码的二维码图片: {}", query);
        UserCouponsVO detail = couponService.receiveDetail(query);

        log.debug("查看优惠券码的二维码图片成功: {}", query);
        return new Result<>(detail);
    }



}
